<?php
class PhanhoipsController extends AppController {

	var $name = 'Phanhoips';
    var $uses= array('Phanhoip','Noidung');
    var $paginate=array('order'=>array('Phanhoip.ngaytao'=>'desc'));
	function index($idnoidung) {
	   //cap nhat so luot xem
        $nd=$this->Noidung->find('first',array(
                'conditions'=>array('id'=>$idnoidung),
                'fields'=>array('luotxem','tieude','noidung'),'recursive'=>-1));
        $so=$nd ['Noidung'] ['luotxem']+1;
        //////////////////////////////
        $this->Noidung->id=$idnoidung;
	    $this->Noidung->saveField('luotxem',$so);
		$this->Phanhoip->recursive = 0;
		$this->set('phanhoips', $this->paginate(array('noidung_id'=>$idnoidung)));
        $this->set(compact('idnoidung','nd'));
        
         $idnhanvien=$this->Session->read('cur_manv');
        $quyen=$this->Session->read('cur_role');
        $this->set(compact('idnhanvien','quyen','idnoidung'));
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid phanhoip', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('phanhoip', $this->Phanhoip->read(null, $id));
	}

	function add($idnoidung) {
	    $this->set(compact('idnoidung'));
		if (!empty($this->data)) {
			$this->Phanhoip->create();
    
            $this->data ['Phanhoip'] ['noidung_id']=$idnoidung;
			$this->data ['Phanhoip'] ['nhanvien_id']=$this->Session->read('cur_manv');
         	$this->data ['Phanhoip'] ['ngaytao']=date('Y-m-d H:m:s');
            
			if ($this->Phanhoip->save($this->data)) {
				$this->Session->setFlash(__('Bài phản hồi đã được tạo', true),'default',array('class'=>'success'));
				$this->redirect(array('action' => 'add',$idnoidung));
			} else {
				$this->Session->setFlash(__('Phản hồi không thể lưu.', true));
			}
		}
	    $nd=$this->Noidung->find('first',array(
                'conditions'=>array('id'=>$idnoidung),
                'fields'=>array('luotxem','tieude','noidung'),'recursive'=>-1));
        $so=$nd ['Noidung'] ['luotxem']+1;
        //////////////////////////////
        $this->Noidung->id=$idnoidung;
	    $this->Noidung->saveField('luotxem',$so);
		$this->Phanhoip->recursive = 0;
		$this->set('phanhoips', $this->paginate(array('noidung_id'=>$idnoidung)));
        $this->set(compact('idnoidung','nd'));
	    
       $idnhanvien=$this->Session->read('cur_manv');
        $quyen=$this->Session->read('cur_role');
        $this->set(compact('idnhanvien','quyen','idnoidung'));
	
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid phanhoip', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Phanhoip->save($this->data)) {
				$this->Session->setFlash(__('The phanhoip has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The phanhoip could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Phanhoip->read(null, $id);
		}
		$noidungs = $this->Phanhoip->Noidung->find('list');
		$nhanviens = $this->Phanhoip->Nhanvien->find('list');
		$this->set(compact('noidungs', 'nhanviens'));
	}

	function delete($id = null,$nd) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for phanhoip', true));
			$this->redirect(array('action'=>'index',$nd));
		}
		if ($this->Phanhoip->delete($id)) {
			$this->Session->setFlash(__('Phản hồi đã xóa ', true));
			$this->redirect(array('action'=>'add',$nd));
		}
		$this->Session->setFlash(__('Phản hồi không thể xóa', true));
		$this->redirect(array('action' => 'index',$nd));
	}
}
